/**
 * Created by wdp on 2019/11/8.
 */

/**
 * setCookie
 * @param name
 * @param value
 * @param expireMillisecond 过期时间（单位毫秒）
 */
export function setCookie(name, value, expireMillisecond) {
  if (!expireMillisecond)
    expireMillisecond = 60*60*1000; //one hours
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expireMillisecond);
  document.cookie = name + "=" + escape(value) + ((expireMillisecond == null) ? "" : ";path=/;expires=" + exdate.toGMTString());
}

/**
 * getCookie
 * @param name
 * @returns {*}
 */
export function getCookie(name) {
  var name = escape(name);
  var allcookies = document.cookie;
  name += "=";
  var pos = allcookies.indexOf(name);
  if (pos != -1) {
    var start = pos + name.length;
    var end = allcookies.indexOf(";", start);
    if (end == -1) {
      end = allcookies.length;
    }
    var value = allcookies.substring(start, end);
    return unescape(value);
  } else {
    return null;
  }
}

/**
 * delCookie
 * @param name
 * @constructor
 */
export function delCookie(name)
{
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = getCookie (name);
  document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}
//storage存储
export function setItem($id, $value) {
  var $_str = JSON.stringify($value);
  localStorage.setItem($id, $_str);
};
//storage读取
export function getItem($id) {
  var $_value;
  var $_str = localStorage.getItem($id);
  if ($_str != null || $_str != "") {
    try {
      $_value = JSON.parse($_str);
    } catch (e) {
      $_value = $_str;
    }
  }
  return $_value
};
//storage移除
export function removeItem ($id) {
  localStorage.removeItem($id);
};
//storage清除
export function clearItem() {
  localStorage.clear();
};
